package com.dgut.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.dto.ContractPageDTO;
import com.dgut.dto.ContractSaveDTO;
import com.dgut.entity.Salescontracts;
import com.dgut.result.PageResult;
import com.dgut.result.Result;
import com.dgut.service.SalescontractsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "销售合同管理接口")
@RequestMapping("/contract")
@Slf4j
public class SaleContractController {
    @Autowired
    private SalescontractsService salescontractsService;

    @ApiOperation("合同分页查询")
    @GetMapping("/page")
    public Result pageQuery(ContractPageDTO contractPageDTO) {
        Page<Salescontracts> page = new Page<>(contractPageDTO.getCurrentPage(), contractPageDTO.getPageSize());
        QueryWrapper<Salescontracts> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(contractPageDTO.getCusName() != null &&
                                contractPageDTO.getCusName().length() > 0,
                        "cus_name", contractPageDTO.getCusName())
                .like(contractPageDTO.getEmpName() != null &&
                                contractPageDTO.getEmpName().length() > 0,
                        "emp_name", contractPageDTO.getEmpName());

        Page<Salescontracts> contractPage = salescontractsService.page(page, queryWrapper);

        return Result.success(new PageResult(contractPage.getTotal(), contractPage.getRecords()));
    }

    @ApiOperation("合同删除")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        salescontractsService.removeBatchByIds(ids);
        return Result.success();
    }


    @ApiOperation("合同添加")
    @PostMapping
    public Result add(@RequestBody ContractSaveDTO contractSaveDTO) {
        log.info("合同添加：{}", contractSaveDTO);
        salescontractsService.saveWithProduct(contractSaveDTO);
        return Result.success();
    }

    @ApiOperation("合同修改")
    @PutMapping
    public Result update(@RequestBody ContractSaveDTO contractSaveDTO) {
        log.info("合同修改：{}", contractSaveDTO);
        salescontractsService.updateWithOrder(contractSaveDTO);
        return Result.success();
    }

    @GetMapping("/{empId}")
    @ApiOperation("根据员工id查询合同")
    public Result getByEmpId(@PathVariable Long empId, Integer currentPage, Integer pageSize) {
        log.info("根据员工id查询合同：{}", empId);
        QueryWrapper<Salescontracts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id", empId);
        Page<Salescontracts> page = salescontractsService.page(new Page<>(currentPage, pageSize), queryWrapper);
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return Result.success(pageResult);
    }

}
